//
//  CharSelectController.m
//  aorc
//
//  Created by Reza Jelveh
//  Copyright 2007
//  License: GPL
//

#import "CharSelectController.h"
#import "AOChatBinding.h"

static CharSelectController *sharedCharSelectManager = nil;

@implementation CharSelectController

+ (CharSelectController*)sharedManager
{
	@synchronized(self) {
		if (sharedCharSelectManager == nil) {
			[[self alloc] init]; // assignment not done here
		}
	}
	return sharedCharSelectManager;
}


+ (id)allocWithZone:(NSZone *)zone
{
	@synchronized(self) {
		if (sharedCharSelectManager == nil) {
			sharedCharSelectManager = [super allocWithZone:zone];
			return sharedCharSelectManager;  // assignment and return on first allocation
		}
	}
	return nil; //on subsequent allocation attempts return nil
}

- (void)setUsers:(NSArray *)charArray
{
	NSEnumerator *enumerator = [charArray objectEnumerator];
	NSDictionary *currentUser;

	[characterId removeAllItems];
	while (currentUser = [enumerator nextObject]) {
		[characterId addItemWithTitle:[currentUser objectForKey:@"User Name"]];
	}
	
	[self show];
}

- (IBAction)connect:(id)sender
{
	NSString * myUser;
	myUser = [characterId titleOfSelectedItem];
	
	[[AOChatBinding sharedManager] selectUser:myUser];
	
	[self hide];
}

- (void)hide
{
	[window orderOut:nil]; // to hide it
}

- (void)show
{
	[window makeKeyAndOrderFront:nil];
}
@end
